﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.TextAsset
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Bindings;

#nullable disable
namespace UnityEngine
{
  /// <summary>
  ///   <para>Represents a raw text or binary file asset.</para>
  /// </summary>
  [NativeHeader("Runtime/Scripting/TextAsset.h")]
  public class TextAsset : Object
  {
    /// <summary>
    ///   <para>The raw bytes of the text asset. (Read Only)</para>
    /// </summary>
    public extern byte[] bytes { [MethodImpl(MethodImplOptions.InternalCall)] get; }

    [MethodImpl(MethodImplOptions.InternalCall)]
    private extern byte[] GetPreviewBytes(int maxByteCount);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Internal_CreateInstance([Writable] TextAsset self, string text);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private extern IntPtr GetDataPtr();

    [MethodImpl(MethodImplOptions.InternalCall)]
    private extern long GetDataSize();

    private static AtomicSafetyHandle GetSafetyHandle(TextAsset self)
    {
      AtomicSafetyHandle ret;
      TextAsset.GetSafetyHandle_Injected(self, out ret);
      return ret;
    }

    /// <summary>
    ///   <para>The text contents of the file as a string. (Read Only)</para>
    /// </summary>
    public string text => TextAsset.DecodeString(this.bytes);

    /// <summary>
    ///   <para>The size of the text asset data in bytes. (Read Only)</para>
    /// </summary>
    public long dataSize => this.GetDataSize();

    /// <summary>
    ///   <para>Returns the contents of the TextAsset.</para>
    /// </summary>
    public override string ToString() => this.text;

    public TextAsset()
      : this(TextAsset.CreateOptions.CreateNativeObject, (string) null)
    {
    }

    /// <summary>
    ///         <para>Create a new TextAsset with the specified text contents.
    /// 
    /// This constructor creates a TextAsset, which is not the same as a plain text file. When saved to disk using the AssetDatabase class, the TextAsset should be saved with the .asset extension.</para>
    ///       </summary>
    /// <param name="text">The text contents for the TextAsset.</param>
    public TextAsset(string text)
      : this(TextAsset.CreateOptions.CreateNativeObject, text)
    {
    }

    internal TextAsset(TextAsset.CreateOptions options, string text)
    {
      if (options != TextAsset.CreateOptions.CreateNativeObject)
        return;
      TextAsset.Internal_CreateInstance(this, text);
    }

    public unsafe NativeArray<T> GetData<T>() where T : struct
    {
      long dataSize = this.GetDataSize();
      long num = (long) UnsafeUtility.SizeOf<T>();
      if (dataSize % num != 0L)
        throw new ArgumentException(string.Format("Type passed to {0} can't capture the asset data. Data size is {1} which is not a multiple of type size {2}", (object) nameof (GetData), (object) dataSize, (object) num));
      long length = dataSize / num;
      NativeArray<T> nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>((void*) this.GetDataPtr(), (int) length, Allocator.None);
      NativeArrayUnsafeUtility.SetAtomicSafetyHandle<T>(ref nativeArray, TextAsset.GetSafetyHandle(this));
      return nativeArray;
    }

    internal string GetPreview(int maxChars)
    {
      return TextAsset.DecodeString(this.GetPreviewBytes(maxChars * 4));
    }

    internal static string DecodeString(byte[] bytes)
    {
      int length = TextAsset.EncodingUtility.encodingLookup.Length;
      for (int index1 = 0; index1 < length; ++index1)
      {
        byte[] key = TextAsset.EncodingUtility.encodingLookup[index1].Key;
        int index2 = key.Length;
        if (bytes.Length >= index2)
        {
          for (int index3 = 0; index3 < index2; ++index3)
          {
            if ((int) key[index3] != (int) bytes[index3])
              index2 = -1;
          }
          if (index2 >= 0)
          {
            try
            {
              return TextAsset.EncodingUtility.encodingLookup[index1].Value.GetString(bytes, index2, bytes.Length - index2);
            }
            catch
            {
            }
          }
        }
      }
      int index = 0;
      return TextAsset.EncodingUtility.targetEncoding.GetString(bytes, index, bytes.Length - index);
    }

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void GetSafetyHandle_Injected(TextAsset self, out AtomicSafetyHandle ret);

    internal enum CreateOptions
    {
      None,
      CreateNativeObject,
    }

    private static class EncodingUtility
    {
      internal static readonly KeyValuePair<byte[], Encoding>[] encodingLookup;
      internal static readonly Encoding targetEncoding = Encoding.GetEncoding(Encoding.UTF8.CodePage, (EncoderFallback) new EncoderReplacementFallback("�"), (DecoderFallback) new DecoderReplacementFallback("�"));

      static EncodingUtility()
      {
        Encoding encoding1 = (Encoding) new UTF32Encoding(true, true, true);
        Encoding encoding2 = (Encoding) new UTF32Encoding(false, true, true);
        Encoding encoding3 = (Encoding) new UnicodeEncoding(true, true, true);
        Encoding encoding4 = (Encoding) new UnicodeEncoding(false, true, true);
        Encoding encoding5 = (Encoding) new UTF8Encoding(true, true);
        TextAsset.EncodingUtility.encodingLookup = new KeyValuePair<byte[], Encoding>[5]
        {
          new KeyValuePair<byte[], Encoding>(encoding1.GetPreamble(), encoding1),
          new KeyValuePair<byte[], Encoding>(encoding2.GetPreamble(), encoding2),
          new KeyValuePair<byte[], Encoding>(encoding3.GetPreamble(), encoding3),
          new KeyValuePair<byte[], Encoding>(encoding4.GetPreamble(), encoding4),
          new KeyValuePair<byte[], Encoding>(encoding5.GetPreamble(), encoding5)
        };
      }
    }
  }
}
